var express = require('express');
var router = express.Router();

const userModels=require('../models/userModels.js')
const studentsModels=require('../models/studentsModels.js')
const moment=require('moment')
// db.set('user.name', 'typicode').write()
const middleware=require('../middleware/DpageMiddleware.js')
const shortid = require('shortid')
const cookieParser=require('cookie-parser')
/* GET home page. */
//登陆页面
router.use(cookieParser())
router.get('/',middleware, function(req, res, next) {
	studentsModels.find().sort({
		time: -1
	}).then((Data) => {
		res.render('students', {
			students: Data,
			moment: moment,
			name:req.cookies.name
		});
	})
});


router.get('/dl', function(req, res, next) {
	//res.cookie("name","zhangsan",1000*60)//cookie的保存时间
	res.render("Dpage",{mima:'请入你的用户名和密码'})
});
//从登录页面跳转到信息页面
router.post('/dl', function(req, res, next) {
	//获取用户名和密码
	let username=req.body.username
	let passwrod=req.body.passwrod
	if(username==""&&passwrod==""){
		res.render("Dpage",{mima:'你输入的用户名或者密码不能为空'})
		}
	userModels.findOne({username:req.body.username}).then((Data) => {
	    if(Data.passwrod===req.body.passwrod&&req.body.passwrod!=null){
			res.cookie('name',req.body.username)
			console.log(cookieParser.signedCookies)
			studentsModels.find().sort({
				time: -1
			}).then((Data) => {
				res.render('students', {
					students: Data,
					moment: moment,
					name:req.body.username
				});
			})
		}else{
			res.render("Dpage",{mima:'你输入的用户名或者密码出错'})
		}
	}).catch(err => {
		res.render("Zpage")
	})
	
});
//注册页面
router.get('/zc', function(req, res, next) {
	res.render("Zpage")
});
router.post('/zc', function(req, res, next) {
	userModels.create({
		 ...req.body
	}).then((data)=>{
			res.render("Dpage",{mima:'请入你的用户名和密码'})
			}).catch(err=>{
			res.status(500).send('插入失败')
			return;
		})  
});

//创建新生
router.get('/student', middleware,function(req, res, next) {
	res.cookie('name',req.cookies.name)
	res.render("student")
});
//创建新生信息
router.post('/student',middleware, function(req, res, next) {
	let id = shortid.generate()
	studentsModels.create({
		 ...req.body,
		 time:moment(req.body.time).toDate()
	}).then((Data)=>{
			res.redirect('/')
			}).catch(err=>{
			res.status(500).send('插入失败')
			return;
		})  
});
//查看学生信息
router.get('/students', middleware,function(req, res, next) {
	res.cookie('name',req.cookies.name)
	res.redirect('/')
})


//删除学生信息
router.get('/students/:id', function(req, res, next) {
	//获取params的id参数
	res.cookie('name',req.cookies.name)
	let id = req.params.id
	//删除
	studentsModels.deleteOne({_id:id}).then((data)=>{
			res.redirect('/')//删除成功跳转页面的设置
			}).catch(err=>{
			res.status(500).send('插入失败')
			return;
		})
	//提醒
	
});
router.get('/students/update/:id', function(req, res, next) {
	//获取params的id参数
	res.cookie('name',req.cookies.name)
	let id = req.params.id
	//查找对应的id记录值，保存下来
	//const acc=db.get('accounts').find({id:id}).value();
	studentsModels.findById({_id:id}).then((data)=>{//查找数据库中与id匹配的帐户信息
			res.render('update',{students:data})
		})
	  .catch(err => {
	    res.status(500).send('读取失败')
	  })
	//提醒
	
});
router.post('/students/:id/update', function(req, res, next) {
	res.cookie('name',req.body.username)
	const id = req.params.id;
	//db.get('accounts').find({id:id}).assign(req.body).write()
	studentsModels.findByIdAndUpdate({_id:id}, req.body,{ new: true })//通过查找到id然后修改信息，
	//{ new: true }作为选项的一部分传递给该方法
	  .then((data)=>{
		  console.log(data)
			res.redirect('/')
		}).catch(err => {
	    res.status(500).send('修改失败')
	  })
})
//退出登录
router.get('/logout', middleware,function(req, res, next) {
	res.clearCookie('name',req.body.username)
	res.render('index', {msg: '退出成功',url: '/dl',xs:"点击回到登陆页面"});
});
module.exports = router;
